package fr.afcepf.atod20.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import fr.afcepf.atod20.model.entity.Personne;
import fr.afcepf.atod20.model.util.SimpleDataSource;


public class DaoPersonneImpl implements IDaoPersonne {

	private final String reqAjout = 
			"INSERT INTO personne"
			+ " (nom, prenom, mail, mdp, date_naissance )"
			+ " VALUES (?,?,?,?,? ) ";
	
	private final int indiceParamNom = 1;
	private final int indiceParamPrenom = 2;
	private final int indiceParamMail = 3;
	private final int indiceParamMdp = 4;
	private final int indiceParamParamDateNaissance = 5;
	
	
	@Override
	public Personne ajouter(Personne pers) {
		DataSource ds = new SimpleDataSource();
		try {
			Connection cnx = ds.getConnection();
			
			PreparedStatement pstmt = cnx.prepareStatement(reqAjout, Statement.RETURN_GENERATED_KEYS);

			pstmt.setString(indiceParamNom, pers.getNom());
			pstmt.setString(indiceParamPrenom, pers.getPrenom());
			pstmt.setString(indiceParamMail, pers.getMail());
			pstmt.setString(indiceParamMdp, pers.getMdp());
			
			if(pers.getDateNaissance() != null){
				pstmt.setDate(indiceParamParamDateNaissance, new java.sql.Date(pers.getDateNaissance().getTime())  );
			} else {
				pstmt.setDate(indiceParamParamDateNaissance, new java.sql.Date(new Date().getTime())  );
			}
			
			int nbLignesAjoutee = pstmt.executeUpdate();
			ResultSet rsKeys = pstmt.getGeneratedKeys();
			
			if(nbLignesAjoutee == 1 & rsKeys.next()) {
				pers.setId(rsKeys.getInt(1));
			}
			
			
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		return pers;
	}

	
	
	private final String reqRechercher = 
			"SELECT * FROM personne WHERE nom like ? ";
	private final int indiceParamNomRechercher = 1;
	private final String colonneId = "id";
	private final String colonneNom = "nom";
	private final String colonnePrenom = "prenom";
	private final String colonneMail = "mail";
	private final String colonneMotDePasse = "mdp";
	private final String colonneDateNaissance = "date_naissance";
	
	@Override
	public List<Personne> rechercher(String nom) {
		List<Personne> liste = new ArrayList<Personne>();
		DataSource ds = new SimpleDataSource();
		
		try {
			Connection cnx = ds.getConnection();
			PreparedStatement pstmt = cnx.prepareStatement(reqRechercher, Statement.RETURN_GENERATED_KEYS);
			
			pstmt.setString(indiceParamNomRechercher, "%" + nom + "%");
			ResultSet rs = pstmt.executeQuery();
			
			while(rs.next()) {
				Personne p = new Personne(
						rs.getInt(colonneId), 
						rs.getString(colonneNom), 
						rs.getString(colonnePrenom), 
						rs.getString(colonneMail), 
						rs.getString(colonneMotDePasse), 
						rs.getDate(colonneDateNaissance) );
				liste.add(p);
			}
			
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		
		
		return liste;
	}

	
	private final String reqDelete = "DELETE FROM personne WHERE id = ?";
	private final int indiceParamIdDelete = 1;
	
	@Override
	public boolean supprimer(Integer idPers) {
		
		DataSource ds = new SimpleDataSource();
		
		try {
			Connection cnx = ds.getConnection();
			PreparedStatement pstmt = cnx.prepareStatement(reqDelete);
			
			pstmt.setInt(indiceParamIdDelete, idPers);
			
			if(pstmt.executeUpdate() == 1){
				return true;
			}
			
			
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		
		
		
		return false;
	}

}


